<?php
class CommentAction extends Action {
	private $uid = '';
	/*======================================================================*\
		Function:	_initialize
		Purpose:	初始化一些参数，主要判断是否登录授权
		Input:		无	            空
		Output:		无      		空
	\*======================================================================*/
	public function _initialize(){
		
		$this->_uid = session('uid');
		if(!$this->_uid) $this->redirect("Public/login");//(503,"对不起，您登录超时了，请重新登录！",1);
		//B('Authenticate', $action);
	}

	/*======================================================================*\
		Function:	reply
		Purpose:	发布评论
		Input:		无	            空
		Output:		ajax      	#
	\*======================================================================*/
	public function reply(){
		$Comment = M('WorklogComment');
		$User = M('UserProfile');
		$param = $this->_param();
		$uid = $this->_uid;
		$worklogID = intval($param['worklog_id']);
		if($worklogID && $param['comment']){
			$data = array(
				'owner_uid'	=> $uid,
				'worklog_id' => $worklogID,
				'comment' => $param['comment'],
				'create_time' => datetime(),
				'update_time' => datetime(),
				'status' => 1
			);
			$replier = $User->field('name')->where("owner_uid = '$uid'")->find();
			$per_comment = array(
				'replier' => $replier['name'],
				'create_time' => datetime(),
				'comment' => $param['comment']
			);
			$v = $Comment->add($data);
			//$sql = $Comment->getLastSql();
			if($v){
				$this->ajaxReturn($per_comment,'添加成功',1);
			}else{
				$this->ajaxReturn(0,'添加失败'.$sql,0);
			}
			
		}else{
			$this->ajaxReturn(0,'不好意思，出错啦',0);
		}
	}
	public function commentList(){
		$Comment = M('WorklogComment');
		$param = $this->_param();
		$worklog_id = $param['worklog_id'];
		$list = $Comment->alias('A')->join('oa_user_profile B ON A.owner_uid = B.owner_uid')->where("A.worklog_id = '$worklog_id'")->field('B.name,A.comment,A.create_time')->select();
		$this->assign("list",$list);
		//$sql = $Comment->getLastSql();
		//echo $sql;
		//dump($list);
		$this->display();
	}
	
}